public class TrapezoidalRule {

   static double f(double x) {
      return (1/(Math.pow(x, 2)+1));
   }


   static double integrate(double a, double b, int N) {
      double h = (b - a) / N;              // step size
      double sum = 0.5 * (f(a) + f(b));    // area
      for (int i = 1; i < N; i++) {
         double x = a + h * i;
         sum = sum + f(x);
      }

      return sum * h;
   }

   public static void main(String[] args) { 
      
      System.out.println("For 10: "); System.out.println(integrate(1, 7, 10));
      System.out.println("For 100: "); System.out.println(integrate(1, 7, 100));
      System.out.println("For 1000: "); System.out.println(integrate(1, 7, 1000));
   }

}